$(function() {
    $( "#tabs" ).tabs();

    window.jsonParser = new JsonParser("");

    $("#secretKey").val($("option:selected", $("#selClient")).attr("secretKey"));

    $("#selClient").change(function(){
        var optionSelected = $("option:selected", this);
        $("#secretKey").val($(optionSelected).attr("secretKey"));

        $("#p_clientId").val( $("#selClient").val());
        $("#p_secretKey").val( $("#secretKey").val());
    });

    $("#p_clientId").val( $("#selClient").val());
    $("#p_secretKey").val( $("#secretKey").val());



});

function uuid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";

    var uuid = s.join("");
    return uuid;
}

function showResult(settings){
    $.fn.mask();
    $("#requestedUrl").val(  $("#requestedUrl").attr("domain") + settings.url);
    $("#requestedMethod").text(settings.type);
    $("#p_data").text(settings.data == null?"":settings.data );
    var timestamp =  Math.round(new Date().getTime());

    var signature = [];
    signature.push("uri=" + settings.url);
    signature.push("&method=" + settings.type.toUpperCase());
    if(settings.data != null && settings.data.length > 0)
        signature.push("&body=" + settings.data);
    signature.push("&timestamp=" + timestamp);
    console.log(signature.join(''));
    var signatureStr =  CryptoJS.HmacSHA1(signature.join(''),$("#secretKey").val()).toString(CryptoJS.enc.Base64);
    console.log(signatureStr);
    $.ajax({
        type: settings.type,
        url:settings.url,
        dataType: settings.dataType,
        data: settings.data,
        contentType: "application/json",
        headers:{
            "Client-Id": $("#selClient").val(),
            "Visitor-Id" : $("#visitorId").val(),
            "Timestamp" : timestamp,
            "Client-Signature": signatureStr,
            "customer-id" : $("#txtcustomerId").val(),
            "Request-Id" : uuid()
        },
        success:function(data){

            window.jsonParser.jsonContent = JSON.stringify(data);
            window.jsonParser.init();

            if(settings.success)
                settings.success(data);

            $.fn.mask.close();
        },
        error: function(data){
            window.jsonParser.jsonContent = JSON.stringify(data);
            window.jsonParser.init();
            $.fn.mask.close();
        }
    });
}


var customer = {
    login: function(){
        var settings = {
            type:"POST",
            url:"/login",
            dataType: "json",
            data:JSON.stringify( {
                "userName" : $("#userName").val(),
                "password" : $("#password").val()
            }),
            success : function(data){
                $("#txtcustomerId").val(data.id);
            }
        };

        showResult(settings);
    },
    logout:function(){
        var settings = {
            type:"GET",
            url:"/logout",
            dataType: "json"
        };

        showResult(settings);
    },
    get : function(){
        var settings = {
            type:"GET",
            url:"/customer",
            dataType: "json",
            success: function(data){

            }
        };
        showResult(settings);
    }
};